﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace DiffLib.Tests.QA
{
    public class QualityTestsBase
    {
        public IEnumerable<Type> AllPublicTypes()
        {
            return from type in typeof(Diff<>).Assembly.GetTypes()
                   where type.IsPublic
                   select type;
        }

        public IEnumerable<MethodInfo> AllMethods()
        {
            return from type in AllPublicTypes()
                   where type.BaseType != typeof(Delegate)
                   where type.BaseType != typeof(MulticastDelegate)
                   from method in type.GetMethods()
                   where method.DeclaringType == type
                   where !method.Name.StartsWith("get_")
                   where !method.Name.StartsWith("set_")
                   select method;
        }

        public IEnumerable<MethodInfo> AllPublicMethods()
        {
            return from method in AllMethods()
                   where method.IsPublic
                   select method;
        }

        public IEnumerable<ConstructorInfo> AllPublicConstructors()
        {
            return from type in AllPublicTypes()
                   where type.BaseType != typeof(Delegate)
                   where type.BaseType != typeof(MulticastDelegate)
                   from ctor in type.GetConstructors()
                   where ctor.DeclaringType == type
                   where ctor.IsPublic
                   select ctor;
        }

        public IEnumerable<PropertyInfo> AllProperties()
        {
            return from type in AllPublicTypes()
                   from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
                   where property.DeclaringType == type
                   select property;
        }

        public IEnumerable<PropertyInfo> AllPublicProperties()
        {
            return from type in AllPublicTypes()
                   from property in type.GetProperties()
                   where property.DeclaringType == type
                   select property;
        }

        public IEnumerable<FieldInfo> AllPublicFields()
        {
            return from type in AllPublicTypes()
                   where !type.IsEnum
                   from field in type.GetFields()
                   where field.DeclaringType == type
                   select field;
        }
    }
}
